﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.IO;
using XWG.JWBST.ProcessInterfaces;
using Newtonsoft.Json.Converters;

namespace XWG.JWBST.VersionProcess
{
    /// <summary>
    /// 版本升级
    /// </summary>
    public class Process : IProcess
    {
        public string ProcessData(StreamReader reader, HttpContext context)
        {
            string receivemes = HttpUtility.UrlDecode(reader.ReadToEnd());
            string json = string.Empty;
            //反序列化json对象
            JObject ob = (JObject)JsonConvert.DeserializeObject(receivemes);
            int VerNo = Convert.ToInt32(ob["VerNo"].ToString().Replace("\"", string.Empty));
            XWG.JWBST.JWBSTEntity.TbBstVersion verinfo = new JWBSTEntity.TbBstVersion();
            verinfo = XWG.JWBST.JWBSTBLL.TbBstVersionBLL.GetLastVno();
             Ver Version = new Ver();
            //无需更新
            if (verinfo.VersionNum <= VerNo)
            {
                Version.Version = "0";
                json = JsonConvert.SerializeObject(Version);
                return json;
            }
            //数据库无数据
            else if (string.IsNullOrEmpty(verinfo.VersionId))
            {       
                return "2";
            }
            //需要更新
            else
            {
                Version.Version = "0";
                json = JsonConvert.SerializeObject(verinfo);
                JObject rejson = (JObject)JsonConvert.DeserializeObject(json);
                rejson.Add("Version", "1");
                IsoDateTimeConverter dtConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
                json = JsonConvert.SerializeObject(rejson,dtConverter);
                return json;
            }
        }
    }

    public class Ver
    {
        public string Version { get; set; }
    }
}
